import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

// _______________________________________________________dayjs
import dayjs from 'dayjs'
Vue.filter('dateFormat', (dt) => {
  return dayjs(dt).format('YYYY-MM-DD HH:mm:ss')
})

// _______________________________________________________富文本
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor)

// _______________________________________________________elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import '@/assets/global.less' 

// ___________________________________________________________ axios
import axios from 'axios'
axios.defaults.baseURL = 'http://www.liulongbin.top:3008'
Vue.prototype.$http = axios

// 请求拦截器
axios.interceptors.request.use(config => {
  if(config.url.startsWith('/my')) {
    config.headers.Authorization = store.state.token
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use(function(response) {
  // 响应状态码为2xxx开头时 触发成功的回调, 形参中的 response 是"成功的结果"
  return response
}, function(error) {
  // 响应状态码不为2xxx开头 触发失败的回调函数
  if(error.response.status === 401) {
    store.commit('updateToken', '')   // 重置为空即为清楚无效token
    router.push('/login')
  }
  return Promise.reject(error)

})



new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
